package com.mask.im.business.strategy.impl.sendcode;

import com.mask.im.business.model.dto.MobileDTO;
import com.mask.im.business.service.RedisService;
import com.mask.im.business.common.util.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import static com.mask.im.business.common.constant.RedisConstant.PHONE_UPDATE_BIND;

@Service("phoneUpdateBindImpl")
public class PhoneUpdateBindImpl extends AbstractSendCode{
	@Autowired
	private RedisService redisService;

	@Autowired
	private TaskExecutor taskExecutor;

	@Override
	public void sendCode(String phone) {
		checkPhoneAndCode(phone,PHONE_UPDATE_BIND);
		//生成验证码
		String code = getCode();

		taskExecutor.execute(()->{
			MobileDTO mobileDTO = MobileDTO.builder().mobile(phone).code(code).build();
			SMSUtil.sendMessage(mobileDTO);
			redisService.set(PHONE_UPDATE_BIND + phone,code,5*60);
		});
	}
}
